package com.jy.horny.adapter

import android.graphics.drawable.Drawable
import android.text.TextUtils
import android.view.View
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.jy.horny.base.adpater.BaseRecyclerAdapter
import com.jy.horny.databinding.ItemRegisterPhotoBinding
import com.jy.horny.model.PhotoModel
import com.jy.horny.utils.Injections.transformRounded14

/**
 * 注册头像上传
 */
class RegisterPhotoAdapter(private var callback: OnItemPhotoClickListener?) :
    BaseRecyclerAdapter<PhotoModel, ItemRegisterPhotoBinding>(ItemRegisterPhotoBinding::inflate) {


    interface OnItemPhotoClickListener {
        fun selectItemPhoto(url: String, position: Int)
        fun deleteItemPhoto(url: String, position: Int)
    }

    override fun onBindView(
        holder: VH<ItemRegisterPhotoBinding>,
        position: Int,
        data: PhotoModel?
    ) {
        holder.binding.txtItemRegisterPhotoCover.visibility =
            if (position == 0) View.VISIBLE else View.GONE
        if (!TextUtils.isEmpty(data?.imgUrl)) {
            holder.binding.imgItemRegisterPhotoAdd.visibility = View.GONE
            holder.binding.progressItemRegisterPhoto.visibility = View.VISIBLE
            Glide.with(holder.binding.imgItemRegisterPhotoUrl.context)
                .load(data?.imgUrl)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(transformRounded14)
                .listener(object : RequestListener<Drawable> {
                    override fun onLoadFailed(
                        e: GlideException?,
                        model: Any?,
                        target: Target<Drawable>?,
                        isFirstResource: Boolean
                    ): Boolean {
                        holder.binding.progressItemRegisterPhoto.visibility = View.GONE
                        holder.binding.imgItemRegisterPhotoDel.visibility =
                            if (position == 0) View.GONE else View.VISIBLE
                        return false
                    }

                    override fun onResourceReady(
                        resource: Drawable?,
                        model: Any?,
                        target: Target<Drawable>?,
                        dataSource: DataSource?,
                        isFirstResource: Boolean
                    ): Boolean {
                        holder.binding.progressItemRegisterPhoto.visibility = View.GONE
                        holder.binding.imgItemRegisterPhotoDel.visibility =
                            if (position == 0) View.GONE else View.VISIBLE
                        return false
                    }

                }).into(holder.binding.imgItemRegisterPhotoUrl)
        } else {
            if (data?.state == true) {
                holder.binding.imgItemRegisterPhotoAdd.visibility = View.GONE
                holder.binding.imgItemRegisterPhotoDel.visibility = View.GONE
                holder.binding.progressItemRegisterPhoto.visibility = View.VISIBLE
            } else {
                holder.binding.imgItemRegisterPhotoAdd.visibility = View.VISIBLE
                holder.binding.imgItemRegisterPhotoDel.visibility = View.GONE
                holder.binding.progressItemRegisterPhoto.visibility = View.GONE
            }
        }
        holder.binding.progressItemRegisterPhoto.visibility =
            if (data?.state == true) View.VISIBLE else View.GONE
        holder.binding.imgItemRegisterPhotoDel.setOnClickListener {
            callback?.deleteItemPhoto(data?.imgUrl ?: "", position)
        }
        holder.itemView.setOnClickListener {
            callback?.selectItemPhoto(data?.imgUrl ?: "", position)
        }
    }

}